package com.brillilab.starter.controller.aio.system;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.common.redis.RedisCache;
import com.brillilab.domain.vo.lab.OrganizationVo;
import com.brillilab.service.core.lab.IOrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zlb
 * @Title: OrgManageController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/7/3 9:54
 */

/**
 * 组织管理API
 */
@RestController
@RequestMapping("/api/organization")
@Slf4j
public class OrgManageController {
    @Autowired
    private IOrganizationService organizationService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 通过父级ID获取组织列表
     * {\"parentId\":\"父级ID 必填顶级地区填0 L\"}
     *
     * @return
     */
    @GetMapping("/list")
    public ResponseVo organizationList(@RequestParam(defaultValue = "-1") Long parentId) {
        List<OrganizationVo> orgList;
        if(redisCache.getObj("orgList")!=null){
            orgList=(List<OrganizationVo>)redisCache.getObj("orgList");
            log.info("机构列表-redis");
        }
        else {
            orgList = organizationService.getOrgList(parentId);
            if(!CollectionUtils.isEmpty(orgList)) {
                redisCache.set("orgList", orgList);
            }
            log.info("机构列表-mysql");
        }
        return ResponseVo.success(orgList);
    }
}
